﻿@using TaskoMask.BuildingBlocks.Contracts.Dtos.Projects
@inject ProjectApiService ProjectApiService
@inject OrganizationApiService OrganizationApiService

<EditForm Model="ProjectModel" OnValidSubmit="HandleAddProject">

    <DataAnnotationsValidator />

    <div class="form-group">
        <InputText @bind-Value="ProjectModel.Name" placeholder="Name" class="form-control"></InputText>
        <ValidationMessage For="()=>ProjectModel.Name" class="text-danger"></ValidationMessage>
    </div>

    <div class="form-group">
        <select @bind="ProjectModel.OrganizationId" class="form-control">
            @foreach (var prop in organizationsSelectList)
            {
                <option value="@prop.Value" selected="@(prop.Value==OrganizationId?true:false)">@prop.Text</option>
            }
        </select>
        <ValidationMessage For="()=>ProjectModel.OrganizationId" class="text-danger"></ValidationMessage>
    </div>

    <div class="form-group">
        <InputText @bind-Value="ProjectModel.Description" placeholder="Description" class="form-control"></InputText>
        <ValidationMessage For="()=>ProjectModel.Description" class="text-danger"></ValidationMessage>
    </div>

    <div class="form-group">
        <button type="submit" class="btn btn-primary mr-1">Add</button>
        <button @onclick="@(()=>ModalInstance.CancelAsync())" class="btn btn-warning text-dark mr-1">Cancel</button>
    </div>

</EditForm>

@code {


    #region Fields and Parameters


    /// <summary>
    /// Default organization for project
    /// </summary>
    [Parameter]
    public string OrganizationId { get; set; }



    /// <summary>
    /// organaizations list to use in DropDownList
    /// </summary>
    IEnumerable<SelectListItem> organizationsSelectList = new List<SelectListItem>();



    /// <summary>
    /// Form Model
    /// </summary>
    AddProjectDto ProjectModel = new AddProjectDto();



    /// <summary>
    /// Access to current modal
    /// </summary>
    [CascadingParameter]
    BlazoredModalInstance ModalInstance { get; set; }




    #endregion Fields and Parameters



    #region Protected Methods


    /// <summary>
    ///
    /// </summary>
    protected override async Task OnInitializedAsync()
    {
        await GetOrganizations();

        SetDefaultOrganizationId();
    }


    #endregion Protected Methods


    #region private Methods


    /// <summary>
    /// get organization select list to use in DropDownList
    /// </summary>
    private async Task GetOrganizations()
    {
        var organizationsResult = await OrganizationApiService.GetSelectListItemsAsync();
        if (!organizationsResult.IsSuccess)
        {
            QueryResultHandler<IEnumerable<SelectListItem>>.Init()
                .WithResult(organizationsResult)
                .ShowErrorToast(ToastService)
                .CloseModal(ModalInstance);
        }
        else if (organizationsResult.Value.Any())
        {
            organizationsSelectList = organizationsResult.Value;
        }

    }




    /// <summary>
    ///
    /// </summary>
    private void SetDefaultOrganizationId()
    {
        if (!string.IsNullOrEmpty(OrganizationId))
            ProjectModel.OrganizationId = OrganizationId;
        else if (organizationsSelectList.Any())
            ProjectModel.OrganizationId = organizationsSelectList.First().Value;
    }




    /// <summary>
    /// 
    /// </summary>
    private async Task HandleAddProject()
    {
        var result = await ProjectApiService.AddAsync(ProjectModel);
        CommandResultHandler.Init()
            .WithResult(result)
            .WithComponentMessageType(MessageType.Project_Added)
            .ShowErrorToast(ToastService)
            .PublishMessage(ComponentMessageService)
            .CloseModal(ModalInstance);
    }




    #endregion private Methods

}